import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;
import *as cc from "cc";
import { EVENT_TYPE, MISSION_TYPE } from '../../Cube/Levels';
import { TiliMgr } from './TiliMgr';
import EventMgr from './EventMgr';

@ccclass('DatasSpace')
export default class DatasSpace {

  //创建本类的全局调用变量
  public static _instance: DatasSpace = null
  public static get Instance() {
    if (null == this._instance) {
      this._instance = new DatasSpace();
    }
    return this._instance;
  }

  /**
   *  0 是 洗牌道具  
   *  1 是 翻牌道具
   *  2 是 消除道具
   *  3 是 撤回道具
   *  4 是 增加计时道具
   */
  propNumArr = [0,0,0,0,0,0,0,0,0];
  dailyItemArr = []
  dailyShareArr = []
  goldNum: number = 0;
  ftNum: number = 3;//今日次数
  piggyBankGlodNum: number = 0;
  piggyBankGold_tick300: number = 0;
  starNum: number = 0;
  isSameDay: boolean = false;
  ShareCount: number = 1;
  private _gameSound: number = 1;

  //首页星星宝箱数量
  homeBox_starNum: number = 0;
  //首页闯关次数
  homeBox_levelPassCountNum: number = 0;
  //玩家关卡
  level: number = 0;

  gamePause: boolean = false;
  /**初始化游戏数据 */
  initData() {
    this.initLogin();
    this.loadPropNum();
    this.loadGoldNum();
    this.loadFtNum();
    this.loadPiggyBankNum();
    this.loadPiggyBankTick300();
    this.loadStarNum();
    this.loadHomeBoxStarNum();
    this.loadHomeBoxLevelPassCount();
    this.loadPlayerLevel();

    this.opeanLuckyBoxRewardCountDown();
  }

  initLogin() {
    TiliMgr.Instance.init();
    let nowTime = Date.now()
    let d1 = new Date(this.loginTime);
    let d2 = new Date(nowTime)
    let _isChecked = d1.getFullYear() == d2.getFullYear() && d1.getMonth() == d2.getMonth() && d1.getDate() == d2.getDate();
    this.isSameDay = _isChecked;
    this.initDailyInfo();

    this.loginTime = nowTime;
    setInterval(() => {
      this.loginTime = Date.now();
    }, 3000);
  }

  /**加载道具数量 */
  loadPropNum() {
    let dd = this.getString("propNum");
    if (dd == "" || dd == null || dd == undefined || dd == 'NaN') {
      this.setString("propNum", JSON.stringify(this.propNumArr));
      //,JSON.stringify(表名)
    } else {
      this.propNumArr = JSON.parse(dd);
      //this.propNumArr = [2,2,2,2,2,0,0,0,0];
      console.log("dat_turret:",this.propNumArr);
    }
  }

  /**设置道具数量 */
  setPropNum() {
    this.setString("propNum", JSON.stringify(this.propNumArr));
  }

  /**加载金币数量 */
  loadGoldNum() {
    this.goldNum = this.getInt("gold", 0);
  }

  /**设置金币数量 */
  setGoldNum() {
    this.setInt("gold", this.goldNum);
  }

  /**加载体力值 */
  loadFtNum() {
    this.ftNum = this.getInt('ftNum', this.ftNum);
  }

  /**设置体力值 */
  setFtNum() {
    EventMgr.emit(EVENT_TYPE.EVENT_UPD_TILI);
    this.setInt('ftNum', this.ftNum);
  }

  /**
   * @fn 读取存钱罐的数据
   * @time 2024 5 29 15:12
   */
  loadPiggyBankNum() {
    this.piggyBankGlodNum = this.getInt('piggy', 0);
  }

  /**
   * @fn 设置存钱罐的数据
   * @time 2024 5 29 15:13
   *  
   */
  setPiggyBankNum() {
    this.setInt('piggy', this.piggyBankGlodNum);
  }

  /**
   * @fn 读取存钱罐300是否被领取
   * @time 2024 5 29 15:30
   */
  loadPiggyBankTick300() {
    this.piggyBankGold_tick300 = this.getInt('tick300', 0);
  }

  /**
   * @fn 设置存钱罐300是否被领取
   * @time 2024 5 29 15:31
   */
  setPiggyBankTick300() {
    this.setInt('tick300', this.piggyBankGold_tick300);
  }

  /**
   * @fn 加载星星数量
   * @time 2024 5 29 16:07
   */
  loadStarNum() {
    this.starNum = this.getInt('StarNum', 0);
  }

  /**
   * @fn 设置星星数量
   * @time 2024 5 29 16:07
   */
  setStarNum() {
    this.setInt('StarNum', this.starNum);
  }

  /**
   * @fn 获得首页星星宝箱星星数量
   * @time 2024 5 30 2:24
   */
  loadHomeBoxStarNum() {
    //this.homeBox_starNum = this.getInt('homeBox_sn', 0);
  }

  /**
   * @fn 设置首页星星宝箱星星数量
   * @time 2024 5 30 2:24
   */
  setHomeBoxStarNum() {
    this.setInt('homeBox_sn', this.homeBox_starNum);
  }

  /**
   * @fn 获取首页闯关宝箱闯关次数
   * @time 2024 5 30 2:26
   */
  loadHomeBoxLevelPassCount() {
    this.homeBox_levelPassCountNum = this.getInt('homeBox_lp', 0);
  }

  /**
   * @fn 设置首页闯关宝箱闯关次数
   * @time 2024 5 30 2:26
   */
  setHomeBoxLevelPassCount() {
    this.setInt('homeBox_lp', this.homeBox_levelPassCountNum);
  }

  /**
   * @fn 获取玩家关卡
   * @time 2024 5 30 3:22
   */
  loadPlayerLevel() {
    //this.level = this.getInt('lv', 0);
  }

  /**
   * 
   * @fn 设置玩家关卡
   * @time 2024 5 30 3:22
   */
  setPlayerLevel() {
    this.setInt('lv', this.level);
  }

  getLuckyBoxRewardSec() {
    const Sec = 20 * 60;
    return this.getInt('luckyBoxReward', Sec);
  }

  private luckyBoxCountdownInterval: any = null;
  opeanLuckyBoxRewardCountDown() {
    if (this.luckyBoxCountdownInterval) {
      clearInterval(this.luckyBoxCountdownInterval);
    }
    this.luckyBoxCountdownInterval = setInterval(() => {
      let time = this.getLuckyBoxRewardSec();
      time--;
      if (time <= 0) {
        time = 0;
        clearInterval(this.luckyBoxCountdownInterval);
      }
      this.setInt('luckyBoxReward', time);
    }, 1000);
  }
  //---------------------------------------------------------------------------------------------------------------------

  setInt(_key, _value) {
    cc.sys.localStorage.setItem(_key, _value.toString());
  }

  getInt(_key, def) {

    let ds = cc.sys.localStorage.getItem(_key);
    if (ds == "" || ds == null || ds == undefined || ds == 'NaN') {
      this.setInt(_key, def);
      ds = def;
    }
    return Number(ds)
  }

  getString(_key) {
    return cc.sys.localStorage.getItem(_key)
  }


  setString(_key, _value) {
    cc.sys.localStorage.setItem(_key, _value.toString())
  }

  addMission_Target(type: MISSION_TYPE, addNum: number) {
    let dataNum = DatasSpace.Instance.getInt('DailyMission_' + type, 0);
    dataNum += addNum;
    DatasSpace.Instance.setInt('DailyMission_' + type, dataNum);
    return dataNum;
  }

  addLongMission_Target(type: MISSION_TYPE, addNum: number) {
    let dataNum = DatasSpace.Instance.getInt('LongMission_' + type, 0);
    dataNum += addNum;
    DatasSpace.Instance.setInt('LongMission_' + type, dataNum);
    return dataNum;
  }

  setDailyItem(id, isGet: boolean) {
    this.dailyItemArr[id] = isGet ? 1 : 0;
    this.setString('DailyMissionConfig', JSON.stringify(this.dailyItemArr));
  }

  getDailyConfig(id) {
    let data = this.getString('DailyMissionConfig');
    if (!data) {
      let dataStr = JSON.stringify(this.dailyItemArr);
      this.setString('DailyMissionConfig', dataStr);
    } else {
      this.dailyItemArr = JSON.parse(data);
    }
    if (id >= this.dailyItemArr.length) {
      this.dailyItemArr.push(0);
      this.setString('DailyMissionConfig', JSON.stringify(this.dailyItemArr));
      return false
    } else {
      return this.dailyItemArr[id] == 1;
    }
  }

  setDailyShare(id, isGet: boolean) {
    this.dailyShareArr[id] = isGet ? 1 : 0;
    this.setString('DailyShareConfig', JSON.stringify(this.dailyShareArr));
  }
  getDailyShare(id) {
    let data = this.getString('DailyShareConfig');
    if (!data) {
      let dataStr = JSON.stringify(this.dailyShareArr);
      this.setString('DailyShareConfig', dataStr);
    } else {
      this.dailyShareArr = JSON.parse(data);
    }
    if (id >= this.dailyShareArr.length) {
      this.dailyShareArr.push(0);
      this.setString('DailyShareConfig', JSON.stringify(this.dailyShareArr));
      return false
    } else {
      return this.dailyShareArr[id] == 1;
    }

  }

  get GameSound() {
    this._gameSound = this.getInt('GameSound', 1);
    return this._gameSound;
  }

  set GameSound(value) {
    this._gameSound = value;
    this.setInt('GameSound', value);
  }
  get loginTime() {
    let loginTime = this.getInt('loginTime', Date.now());
    return loginTime;
  }

  set loginTime(value) {
    //console.log('loginTime', value)
    this.setInt('loginTime', value);
  }

  initDailyInfo() {
    if (!this.isSameDay) {
      let keys = Object.keys(MISSION_TYPE).filter(key => isNaN(Number(key)));
      for (let i = 0; i < keys.length; i++) {
        let type = 'DailyMission_' + i;
        DatasSpace.Instance.setInt(type, 0);
      }
    }
  }
}


